
$(document).ready(function(){
    // render
    loadData();
    // event
    bindEvent();
});

function loadData() {
    ISS.BACKEND.request({
        url: '../../service/admin/ad_GetPostCategory',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapLevelToView(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: '../../service/admin/ad_GetAllPost',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapAllPostToView(responseContent);
        }
    });
}

function bindEvent() {
    ISS.EVENT.bindEvent('#AdPost_Add', 'click', function () {
        if(validate()){
            sendInfo();
        }
        else {
            var htmlText = '<div class="box s-error mbs msgBox l-pageWrapper mbm">\
                                <div class="pam">Tạo bài viết mới thất bại</div>\
                            </div>';
            ISS.DOM.before('.l-register__benefits', htmlText);
        }
    });

    ISS.EVENT.bindEvent('#PostCommon_Modified', 'click', function () {
        toEditPage();
    });
}

function sendInfo() {
    var post = {};
    var category = {};

    post.maBV = ISS.DOM.getVal('#AdPost_Code');
    post.tieuDe = ISS.DOM.getVal('#AdPost_Title');
    post.tomTat = ISS.DOM.getVal('#AdPost_Summary');
    post.ngayViet = ISS.DOM.getVal('#AdPost_Date');
    post.noiDung = ISS.DOM.getVal('#AdPost_Content');
    post.hinhAnh = ISS.DOM.getVal('#AdPost_Picture');
    category.maDMBV = ISS.DOM.getVal('#AdPost_Category');
    post.danhMucBaiViet = category;

    ISS.BACKEND.request({
        url: '../../service/admin/header/adPost',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(post),
        success: function (data) {
            var responseContent = data.responseObject;
        }
    });
}

function toEditPage() {
    var _maBV = [];
    ISS.DOM.forEach('#form-PostCommon input[type="checkbox"]:checked', function(){
        _maBV.push($(this).val());
    });

    if(_maBV.length == 1) {
        var _url = '/service/admin/toPostPage/' + _maBV[0];
        ISS.WINDOW.location(_url);
    }
    else {
        alert('Bạn chưa chọn hoặc đã chọn hơn 1 dòng');
    }
}

function clearValidate() {
    ISS.DOM.removeClass('#AdPost_Code', 'error');
    ISS.DOM.removeClass('#AdPost_Title', 'error');
}

function validate() {
    var result = true;
    return result;
}

function mapLevelToView(category) {
    var html = '<option selected="selected" value="default_value">Mời chọn...</option>';
    for(var i=0; i<category.length; i++) {
        html += '<option value="' + category[i].maDMBV + '">' + category[i].tenDM + '</option>';
    }
    ISS.DOM.html('#AdPost_Category', html);
}

function mapAllPostToView(post) {
    var rows = post.length;
    var columns = 1;
    var str = '';
    str += '<table class="scroll align-center">';
    str += '<thead>';
    str += '   <tr>';
    str += '       <th class="columnSTT">STT</th>';
    str += '       <th class="columnStick">Tick</th>';
    str += '       <th class="columnStick">Mã bài viết</th>';
    str += '       <th class="columnProductDetail">Danh mục</th>';
    str += '       <th class="columnList">Tiêu đề</th>';
    str += '       <th class="columnTitle-summary">Tóm tắt</th>';
    str += '       <th class="columnColor">Ngày viết</th>';
    str += '       <th class="columnSize">Nội dung</th>';
    str += '       <th class="columnSexual">Hình ảnh</th>';
    str += '   </tr>';
    str += '</thead>';
    str += '<tbody>';
    for(var i = 0; i < rows; i++){
        if (i%2==0){
            str += '<tr class="column-Dark">'
        }else{
            str += '<tr class="column-Light">'
        }
        for (var j = 0; j < columns; j++){
            str += '<td class="columnSTT">' + (i+1) + '</td>';
            str += '<td class="columnStick"><input id="' + post[i].maBV + '" type="checkbox" value="' + post[i].maBV + '"/></td>';
            str += '<td class="columnStick" id="' + post[i].maBV + '">' + post[i].maBV + '</td>';
            str += '<td class="columnProductDetail" id="' + post[i].danhMucBaiViet.maDMBV + '">' + post[i].danhMucBaiViet.tenDM + '</td>';
            str += '<td class="columnList">' + post[i].tieuDe + '</td>';
            str += '<td class="columnTitle-summary">' + post[i].tomTat + '</td>';
            str += '<td class="columnColor">' + post[i].ngayViet + '</td>';
            str += '<td class="columnSize">' + post[i].noiDung + '</td>';
            str += '<td class="columnSexual">' + post[i].hinhAnh + '</td>';
        }
        str += '</tr>';
    }
    str +=   '</tbody>';
    str += '</table>';

    ISS.DOM.html('#PostCommon_All', str);

}